package com.srq.dao.impl;

import com.srq.dao.GoodsDao;
import com.srq.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class GoodsDaoImpl implements GoodsDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Goods> findAllGoods() {
        List<Goods> goodsList = jdbcTemplate.query("select * from goods", new BeanPropertyRowMapper<Goods>(Goods.class));
        return goodsList;
    }

    @Override
    public boolean findGoods(Goods goods) {
        List<Goods> goodsList = jdbcTemplate.query("select * from goods where name=?", new BeanPropertyRowMapper<Goods>(Goods.class),goods.getName());
        if (goodsList==null||goodsList.size()==0){
            return true;
        }
        return false;
    }

    @Override
    public void addGoods(Goods goods) {
        jdbcTemplate.update("insert into goods values (?,?,?,?,?)",null,goods.getName(),goods.getNumber(),goods.getPrice(),goods.getKucun());
    }

    @Override
    public List<Goods> findGoodsByName(String name) {
       // return jdbcTemplate.query("select * from goods where name=?", new BeanPropertyRowMapper<Goods>(Goods.class), name);
        return jdbcTemplate.query("select * from goods where name like ?", new BeanPropertyRowMapper<Goods>(Goods.class), "%"+name+"%");

    }

    @Override
    public void deleteGoods(Integer id) {
        jdbcTemplate.update("delete from goods where id=?",id);


    }
}
